<?php

namespace app\api\model;

use app\common\model\BaseModel;

class CarCate extends BaseModel
{
    // 表名
    protected $name = 'car_cate';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    // 追加属性
    protected $append = [
    ];

    /**
     * 车辆车型列表
     */
    public function cateTypeList($requestData, $user){
        $brand_id = $requestData['brand_id']??0;
        $where = ['type' => 3];
        if($brand_id){
            $where['pid'] = $brand_id;
        }
        $list = self::getList($where, 'id,cate_name', 'sort desc,id desc');
        return ['status' => 1, 'message' => '操作成功', 'result' => $list];
    }

    /**
     * 车辆品牌和配置.
     */
    public function carCate($user){
        $brand_list = self::getList(['type' => 1], 'id,cate_name', 'sort desc,id desc');
        $configure_list = self::getList(['type' => 2], 'id,cate_name', 'sort desc,id desc');
        $label_list = Label::field('id,name')->where(['is_delete' => 1])->order('createtime', 'desc')->select();
        $insure_list = Insure::field('id,insure_name,damage_amount,damage_note,other_amount,other_note,parking_type,parking_note,depreciation_type,depreciation_note')->where(['is_delete' => 1, 'status' => 1])->order('createtime', 'asc')->select();
        foreach ($insure_list as $key => $value){
            $insure_list[$key]['price'] = '';
        }
        $list = [
            'brand_list' => $brand_list,
            'configure_list' => $configure_list,
            'label_list' => $label_list,
            'insure_list' => $insure_list
        ];
        return ['status' => 1, 'message' => '操作成功', 'result' => $list];
    }

    /**
     * 获取列表
     * @param array $where
     * @param string $field
     * @return Driver[]|array|\think\Collection
     */
    public static function getList($where = [], $field = '*', $order = 'createtime desc'){
        return self::where($where)->field($field)->order($order)->order($order)->select();
    }

    /**
     * 获取单条
     * @param array $where
     * @param string $field
     * @return Driver|array|mixed|\think\Model|null
     */
    public static function getFind($where = [], $field = '*'){
        return self::where($where)->field($field)->find();
    }
}
